<?php
class Approval {
    var $id;
    var $matchId;
    var $expectedCoachId;
    var $actualCoachId = NULL;
    var $approvalType;
    var $timestamp = NULL;

    function Approval() {

    }

    function init($row) {
        $i = 0;
        $this->id = $row[$i++];
        $this->matchId = $row[$i++];
        $this->expectedCoachId = $row[$i++];
        $this->actualCoachId = $row[$i++];
        $this->approvalType = $row[$i++];
        $this->timestamp = $row[$i++];
    }

    function canApprove() {
        global $loggedin;
        return (!$this->isApproved()) and (isCommissioner() or (isset ($loggedin) and $loggedin->id == $this->expectedCoachId));
    }

    function isApproved() {
        return isset ($this->actualCoachId);
    }

    function approve() {
        global $loggedin;
        if ($this->canApprove()) {
            $this->actualCoachId = $loggedin->id;
            $this->timestamp = strftime("%Y-%m-%d %H:%M:%S");
            return true;
        } else {
            return false;
        }
    }

    function unapprove() {
        $this->actualCoachId = NULL;
        $this->timestamp = NULL;
    }

    function isHome() {
        return 0 == strcmp("home", $this->approvalType);
    }

    function isAway() {
        return 0 == strcmp("away", $this->approvalType);
    }

    function isCommissioner() {
        return 0 == strcmp("commissioner", $this->approvalType);
    }
}

class ApprovalDao {
    var $home = "home";
    var $away = "away";
    var $commissioner = "commissioner";

    var $selectBase = "select id, fk_match_id, expected_coach_id, actual_coach_id, approval_type, approved_date from match_approvals";

    function hasApprovals() {
        global $loggedin;
        if (!isset ($loggedin)) {
            return false;
        }
        $rs = executeQuery("select count(1) from match_approvals where actual_coach_id is null and expected_coach_id=$loggedin->id");
        $row = mysql_fetch_row($rs);
        return $row[0];
    }

    function isLastApproval($approval) {
        $rs = executeQuery("select count(1) from match_approvals where actual_coach_id is null and fk_match_id=$approval->matchId and id <> $approval->id");
        $row = mysql_fetch_row($rs);
        return 0 == $row[0];
    }

    function findOutstandingCoachApprovals() {
        global $loggedin;
        $rs = executeQuery($this->selectBase . " where actual_coach_id is null and expected_coach_id=$loggedin->id and approval_type in ('$this->home', '$this->away')");
        return $this->getResults($rs);
    }

    function findOutstandingCommissionerApprovals() {
        if (!isCommissioner()) {
            return array ();
        }

        $rs = executeQuery($this->selectBase . " where actual_coach_id is null and approval_type in ('$this->commissioner')");
        return $this->getResults($rs);
    }

    function findMatchApprovals($match) {
        $rs = executeQuery($this->selectBase . " where fk_match_id=$match->id");
        return $this->getResults($rs);
    }

    function findOutstandingMatchApprovals($match) {
        $rs = executeQuery($this->selectBase . " where actual_coach_id is null and fk_match_id=$match->id");
        return $this->getResults($rs);
    }

    function deleteApprovals($matchId) {
        return executeQuery("delete from match_approvals where fk_match_id=$matchId");
    }

    function getResults($rs) {
        $array = array ();
        while ($row = mysql_fetch_row($rs)) {
            $approval = new Approval();
            $approval->init($row);
            $array[$approval->id] = $approval;
        }

        return $array;
    }

    function save($approval) {
        if (NULL == $approval->actualCoachId) {
            return executeQuery("update match_approvals set actual_coach_id=NULL, approved_date=NULL where id=$approval->id");
        } else {
            return executeQuery("update match_approvals set actual_coach_id=$approval->actualCoachId, approved_date='$approval->timestamp' where id=$approval->id");
        }
    }

    function create($approval, $coach = null) {
        $rs = executeQuery("insert into match_approvals (fk_match_id, expected_coach_id, approval_type) " .
        "values ($approval->matchId, $approval->expectedCoachId, '$approval->approvalType')");
		
		global $loggedin, $leagueNameShort, $_SERVER, $rootDir;
        if ($rs && $loggedin->id != $approval->expectedCoachId) {
        	if (!isset($coach)) {
        	    $coach = loadCoach($approval->expectedCoachId);
        	}
        	$subject = "$leagueNameShort match report waiting for your approval";
        	$URL = "http://" . $_SERVER["HTTP_HOST"] . "/" . $rootDir . "match_view.php?matchId=" . $approval->matchId;
            $message = "You have a new $leagueNameShort match report waiting for your approval\n"
            . "You can access it at $URL";

            $headers = "From: " . $loggedin->email . "\r\n" .
            "Reply-To: " . $loggedin->email . "\r\n" .
            "X-Mailer: PHP/" . phpversion();

            mail($coach->email, $subject, $message, $headers);
        }
        return $rs;
    }
}
?>